# Maintainer: Matthias Gondan <Matthias.Gondan-Rochon@uibk.ac.at>

_realname=swi-prolog
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-full" "${MINGW_PACKAGE_PREFIX}-${_realname}-core" "${MINGW_PACKAGE_PREFIX}-${_realname}-packages" "${MINGW_PACKAGE_PREFIX}-${_realname}-archive" "${MINGW_PACKAGE_PREFIX}-${_realname}-bdb" "${MINGW_PACKAGE_PREFIX}-${_realname}-pcre2" "${MINGW_PACKAGE_PREFIX}-${_realname}-yaml" "${MINGW_PACKAGE_PREFIX}-${_realname}-ssl" "${MINGW_PACKAGE_PREFIX}-${_realname}-x" "${MINGW_PACKAGE_PREFIX}-${_realname}-python" "${MINGW_PACKAGE_PREFIX}-${_realname}-doc" "${MINGW_PACKAGE_PREFIX}-${_realname}-examples" "${MINGW_PACKAGE_PREFIX}-${_realname}-tests")
pkgver=9.2.9
pkgrel=1
pkgdesc="Prolog environment (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.swi-prolog.org/"
msys2_references=("cpe: cpe:/a:swi-prolog:swi-prolog")
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-readline: support for readline")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-readline"
             "${MINGW_PACKAGE_PREFIX}-libarchive"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-db"
             "${MINGW_PACKAGE_PREFIX}-libyaml"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-openjpeg2"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://www.swi-prolog.org/download/stable/src/swipl-${pkgver}.tar.gz")
sha256sums=('53f428e2d9bbdf30e53b06c9c42def9a13ff82fc36a111d410fc8b0bc889ee2d')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DMSYS2=1 \
    "${extra_config[@]}" \
    -DUSE_GMP=ON \
    -DSWIPL_PACKAGES_JAVA=OFF \
    -DSWIPL_PACKAGES_ODBC=OFF \
    -DBUILD_TESTING=ON \
    -DINSTALL_TESTS=ON \
    -DSWIPL_{SHARED,STATIC}_LIB=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../swipl-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  PYTHONHOME="${MINGW_PREFIX}" "${MINGW_PREFIX}"/bin/ctest
}

package_swi-prolog-core() {
  pkgdesc="SWI-Prolog core system and compiler"
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Core_system -P cmake_install.cmake

  # Remove tests, they are in a separate package
  rm -Rf "${pkgdir}${MINGW_PREFIX}/lib/swipl/test"

  # Remove path reference in cmake and .pc files
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/**/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/share/pkgconfig/*.pc; do
    sed -s "s|${MINGW_PREFIX}/lib|\${prefix}/lib|g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}"/swipl-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}

package_swi-prolog-packages() {
  pkgdesc="Base packages for SWI-Prolog"
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Core_packages -P cmake_install.cmake

  # Remove tests, they are in a separate package
  rm -Rf "${pkgdir}${MINGW_PREFIX}/lib/swipl/test"
}

package_swi-prolog-archive() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-libarchive")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Libarchive binding for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Archive_interface "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Archive_interface -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Archive_interface${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-python() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-python")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Python binding for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Python_interface "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Python_interface -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Python_interface${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-x() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-openjpeg2")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Graphics subsystem for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Graphics_subsystem "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Graphics_subsystem -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Graphics_subsystem${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-bdb() {
  depends=("${MINGW_PACKAGE_PREFIX}-db"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-core")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Berkeley DB interface for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/BerkeleyDB_interface "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=BerkeleyDB_interface -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/BerkeleyDB_interface${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-pcre2() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-pcre2")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Regular expressions for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Perl_regex "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Perl_regex -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Perl_regex${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-yaml() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-libyaml")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="YAML support in SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/YAML_support "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=YAML_support -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/YAML_support${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-ssl() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-packages"
           "${MINGW_PACKAGE_PREFIX}-openssl")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="OpenSSL/LibreSSL binding for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/OpenSSL_interface "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=OpenSSL_interface -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/OpenSSL_interface${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-doc() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-packages")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Documentation for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Documentation "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Documentation -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Documentation${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-examples() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-packages")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Examples for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${srcdir}"/Examples "${MINGW_PREFIX}"/bin/cmake -DCMAKE_INSTALL_COMPONENT=Examples -P cmake_install.cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  cp -R "${srcdir}/Examples${MINGW_PREFIX}"/lib/swipl/* "${pkgdir}${MINGW_PREFIX}"/lib/swipl/
}

package_swi-prolog-tests() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-packages")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<9.2.6")
  pkgdesc="Unit tests for SWI-Prolog"

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="$srcdir/install-tests" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/swipl"
  rm -Rf "$srcdir/install-tests${MINGW_PREFIX}/lib/swipl/test/packages/swipy/tests/__pycache__"
  rm -Rf "$srcdir/install-tests${MINGW_PREFIX}/lib/swipl/test/packages/swipy/xsb_tests/__pycache__"
  mv "$srcdir/install-tests${MINGW_PREFIX}/lib/swipl/test" "${pkgdir}${MINGW_PREFIX}/lib/swipl/"
}

package_swi-prolog-full() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-core"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-packages"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-archive"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-bdb"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-pcre2"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-yaml"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-ssl"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-x"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-python"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-doc"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-examples"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-tests")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  pkgdesc="SWI-Prolog (full installation)"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
